import * as bridgeService from '../../services/design/bridgeServices';
import { notification } from 'antd';
export default {
    namespace: 'bridge',
    state: {
        title: '',
        currentBridge: null,
        bridges: [],
    },
    reducers: {
        addbridge(state, { payload: bridge }) {
            return { ...state, bridges: [...(state.bridges && state.bridges.length ? state.bridges : []), bridge] };
        },
        updateBridges(state, { payload: bridges }) {
            return { ...state, bridges: bridges ? bridges : [] };
        },
        setCurrent(state, { payload: bridge }) {
            return { ...state, currentBridge: bridge };
        }
    },
    effects: {
        *add({ payload: { values, onSuccess, onError } }, { call, put, select }) {
            const res = yield call(bridgeService.addBridge, { ...values });
            if (res && res.data && res.data.code == "200") {
                notification.info({ message: "添加桥梁成功" });
                onSuccess(res.data.code);
                yield put({
                    type: 'setCurrent',
                    payload: res.data.data
                });

                yield put({
                    type: 'addbridge',
                    payload: res.data.data
                });
            } else {
                onError(res && res.data ? res.data.message : '服务器连接失败');
            }
        },
        *deleteBridge({ payload: { values, onSuccess, onError } }, { call, put, select }) {
            const res = yield call(bridgeService.deleteBridge, { ...values });
            if (res && res.data && res.data.code == "200") {
                notification.info({ message: "删除桥梁成功" });
                onSuccess(res);
            } else {
                onError(res && res.data ? res.data.message : '服务器连接失败');
            }
        },
        *addBridgeMain({ payload: { values, onSuccess, onError } }, { call, put, select }) {
            const res = yield call(bridgeService.addBridgeMain, { ...values });
            if (res && res.data && res.data.code == "200") {
                notification.info({ message: "添加桥梁成功" });
                onSuccess(res);
            } else {
                onError(res && res.data ? res.data.message : '服务器连接失败');
            }
        },
        *getMainBridge({ payload: values }, { call, put, select }) {
            const res = yield call(bridgeService.getMainBridge, { ...values });
            if (res && res.data && res.data.code == "200") {
            } else {

            }
            return res && res.data && res.data.data ? res.data.data : null
        },
        *getBridgeMainList({ payload: values }, { call, put, select }) {
            const res = yield call(bridgeService.getBridgeMainList, { ...values });
            if (res && res.data && res.data.code == "200") {
            } else {

            }
            return res && res.data && res.data.data ? res.data.data : null
        },
        *bridgeMainAndChild({ payload: values }, { call, put, select }) {
            const res = yield call(bridgeService.bridgeMainAndChild, { ...values });
            if (res && res.data && res.data.code == "200") {
            } else {

            }
            return res && res.data && res.data.data ? res.data.data : null
        },
        *queryRoadLimit({ payload: values }, { call, put, select }) {
            const res = yield call(bridgeService.queryRoadLimit, values);
            if (res && res.data && res.data.code == '200') {

            } else {
            }
            return res.data?res.data.data:null
        },
        *caculateDefinitionInstanceProperty({ payload: { values, onSuccess, onError } }, { call, put, select }) {
            const res = yield call(bridgeService.caculateDefinitionInstanceProperty, { ...values });
            if (res && res.data && res.data.code == "200") {
                notification.info({ message: "生成桥梁成功!" });
                onSuccess("生成桥梁成功");
            } else {
                onError(res && res.data ? res.data.message : '服务器连接失败');
            }
        },
        *edit({ payload: { values, onSuccess, onError } }, { call, put, select }) {
            const res = yield call(bridgeService.editBridge, { ...values });
            if (res && res.data && res.data.code == "200") {
                notification.info({ message: "更新桥梁成功" });
                onSuccess(res);
                yield put({
                    type: 'setCurrent',
                    payload: res.data.data
                });
            } else {
                onError(res && res.data ? res.data.message : '服务器连接失败');
            }
        },
        /**查询项目所有桥梁工程信息 */
        *loadProjectBridges({ payload: values }, { call, put }) {
            const res = yield call(bridgeService.queryProjectBridges, values);
            if (res && res.data && res.data.code == "200") {
                if (res.data.data && res.data.data.rows && res.data.data.rows.length) {
                    for (let i = 0; i < res.data.data.rows.length; i++) {
                        let item = res.data.data.rows[i];
                        item.title = ' 桥梁 : ' + item.name;
                        item.key = 'bridge' + item.id;
                        item.eventType = 'bridge'
                        item.eventKey = 'bridge'
                        item.checkable = false

                    }
                    yield put({
                        type: 'updateBridges',
                        payload: res.data.data.rows
                    });
                    return res.data.data.rows;
                }

            } else {
                yield put({
                    type: 'updateBridges',
                    payload: []
                });
                return []
            }
        },
        *calculatePierHeight({ payload: {values,onSuccess,onError} }, { call, put }) {
            const res = yield call(bridgeService.calculatePierHeight, values);
            if (res && res.data && res.data.data && res.data.code == "200") {
                onSuccess(res)
                notification.info({ message: "计算构件成功!" });
            } else {
                onError(res && res.data ? res.data.message : '服务器连接失败');
            }
            return res&&res.data?res.data.data:null
        },
        *queryBridgeDetail({ payload: values }, { call, put }) {
            const res = yield call(bridgeService.queryBridgeDetail, values);
            if (res && res.data && res.data.data && res.data.code == "200") {
                yield put({
                    type: 'refRoads/updateCurrent',
                    payload: res.data.data
                });
                let spans = res.data.data.spans
                if (spans && spans.length) {
                    for (let i = 0; i < spans.length; i++) {
                        let item = spans[i];
                        item.bridgeId = values.id;
                    }
                    yield put({
                        type: 'refSpans/updatebuildingSpans',
                        payload: spans
                    });
                }
                return res.data.data
            } else {
                return []
            }
        },
        *updateCurrent({ payload: bridge }, { call, put }) {
            yield put({
                type: 'setCurrent',
                payload: bridge
            });
        }
    }
}